[PHP] Read and write to a file while keeping lock

Posted by Znarkus on Stack Overflow See other posts from Stack Overflow or by Znarkus
Published on 2010-03-15T22:15:29Z Indexed on 2010/03/15 22:19 UTC
Read the original article Hit count: 251

Filed under:
|
|

Hi!

I am making a simple page load counter by storing the current count in a file. This is how I want to do this:

  1. Lock the file
  2. Read the current count
  3. Increment it
  4. Write new count
  5. Unlock file/close it

Can this be done?

As I understand it, the file can't be written to without losing the lock. The only way I have come up with to tackle this, is to write a character using "r+" mode, and then counting characters.

© Stack Overflow or respective owner

Related posts about php

Related posts about file